package com.huwaiwai001.netty.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * @author 歪大哥😁
 * @date 2020-07-07.
 */
public class FileChannelTransfer {

    public static void main(String[] args) throws Exception {
        String srcPath = "/Users/kathy/studyCode/common/src/main/java/com/huwaiwai001/netty/nio/file01.txt";
        String targetPath = "/Users/kathy/studyCode/common/src/main/java/com/huwaiwai001/netty/nio/file01_back2.txt";


        File srcFile = new File(srcPath);
        File targetFile = new File(targetPath);

        // 创建一个输出流
        try (FileInputStream fileInputStream = new FileInputStream(srcFile);
             FileOutputStream fileOutputStream = new FileOutputStream(targetFile)) {
            // 获取 输入channel  和  输出channel
            FileChannel srcChannel = fileInputStream.getChannel();
            FileChannel targetChannel = fileOutputStream.getChannel();
            // 直接从输入channel中复制数据 到输出channel中 不需要借助buffer
            targetChannel.transferFrom(srcChannel, 0, srcChannel.size());
        }
    }


}
